from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from myadmin import models
from django.core.urlresolvers import reverse
# Create your views here.
def myhome_info(request):
	gid = request.GET.get('gid')
	gobj = models.Goods.objects.get(id=gid)
	return render(request,'myhome/goodsinfo.html',{'ginfo':gobj})

def addcar(request):
	try:
		info = request.GET.dict()
		# 获取商品的对象
		gobj = models.Goods.objects.get(id=info['gid'])
		# 获取用户对象
		uobj = models.Users.objects.get(id=request.session['userinfo']['uid'])

		# 判断 当前用户 有没有添加当前的这个商品
		flag = models.Car.objects.filter(uid=uobj.id).filter(gid=gobj.id)
		if flag.count():
		# 获取商品左更新
			for i in flag:
				i.num+=int(info['num'])
				i.save()
		else:
		# 入库
			car = models.Car()
			car.num=int(info['num'])
			car.gid=gobj
			car.uid=uobj
			car.save()

		return JsonResponse({"msg":1,'info':'添加成功'})
	except :
		return JsonResponse({"msg":1,'info':'添加失败'})

def carpage(request):
	uid = request.session.get('userinfo')
	if not uid:
		return HttpResponse('<script>alert("没有登录");location.href="'+reverse('myhome_login')+'"</script>')
	user = models.Users.objects.get(id=uid['uid'])
	cgoods = user.car_set.all()

	return render(request,'myhome/carpage.html',{'cgoods':cgoods})	

def caredit(request):
	cinfo = request.GET.dict()
	cobj = models.Car.objects.get(id=cinfo['cid'])
	cobj.num=int(cinfo['num'])
	cobj.save()

	return JsonResponse({'error':1,'msg':'修改成功'})


# queren
def confirm(request):
	cart = request.GET.get('cid').split(',')
	cargoods = models.Car.objects.filter(id__in=cart)

	# # 返回一级城市的数
	citys = models.Citys.objects.filter(upid=0)

	# 当前用户的收货地址
	userobj = models.Users.objects.get(id=request.session['userinfo']['uid'])
	address = userobj.address_set.all()


	return render(request,'myhome/pay.html',{'cargoods':cargoods,'citys':citys,'address':address})
# 城际联动
def getcitys(request):
	upid = request.GET['upid']
	citys = models.Citys.objects.filter(upid=upid).values()
	return JsonResponse(list(citys),safe=False)

def saveaddress(request):
    # 接受
    addinfo = request.GET.dict()
    # 存数据
    address = models.Address()
    address.name=addinfo['name']
    address.phone=addinfo['phone']
    address.sheng=models.Citys.objects.get(id=addinfo['sheng']).name

    address.shi=models.Citys.objects.get(id=addinfo['shi']).name
   
    address.xian=models.Citys.objects.get(id=addinfo['xian']).name
  
    address.addinfo=addinfo['addinfo']
    address.uid = models.Users.objects.get(id=request.session['userinfo']['uid'])
    address.save()
    return JsonResponse({'error':0,'msg':'添加成功'})

# 生成订单
def createorder(request):
    oinfo = request.POST.dict()
    print(oinfo)
    # 订单
    order = models.Order()
    order.uid = models.Users.objects.get(id = request.session['userinfo']['uid'])
    order.phone = models.Address.objects.get(id=oinfo['dizhi']).phone
    order.name = models.Address.objects.get(id=oinfo['dizhi']).name
    # 地址
    sheng = models.Address.objects.get(id=oinfo['dizhi']).sheng
    shi = models.Address.objects.get(id=oinfo['dizhi']).shi
    xian = models.Address.objects.get(id=oinfo['dizhi']).xian
    addinfo = models.Address.objects.get(id=oinfo['dizhi']).addinfo
    print(addinfo)
    order.addinfo = sheng+shi+xian+addinfo

    order.wl = int(oinfo['wuliu'])
    order.pay = int(oinfo['zhifu'])

    order.total=0
    order.save()

    
    total=0
    carts = models.Car.objects.filter(id__in=oinfo['car'].split(','))
    for i in carts:
        # 订单详情
        orderinfo = models.Orderinfo()
        orderinfo.orderid = order
        orderinfo.num = i.num
        orderinfo.price = i.gid.price
        orderinfo.gid = i.gid
        orderinfo.save()
        total += i.num*i.gid.price
        i.delete()

    order.total = total
    order.save()
    return HttpResponse('<script>alert("成功添加商品");location.href="'+reverse('myhome_geren')+'"</script>')

    
    


# 删除成功
def shanchu(request):
	cid=request.GET.dict()
	ccar = models.Car.objects.get(id=cid['cid'])
	ccar.delete()
	return redirect(reverse('myhome_carpage'))













